﻿using System;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;


using System.ComponentModel.Composition;
using EnergyService_CSharp;
using System.Windows.Forms;

namespace WebSvcPlugin
{
    [Export(typeof(IEnergyMonitorPlugin))]
    public class WebServiceUpdater : IEnergyMonitorPlugin
    {
        string baseUrl;

        #region IEnergyMonitorPlugin Members

        string IEnergyMonitorPlugin.Name
        {
            get { return "WebSvcPlugin"; }
        }

        string IEnergyMonitorPlugin.Description
        {
            get { return "Post your current reading to a WCF WebService"; }
        }

        bool IEnergyMonitorPlugin.Enabled
        {
            get { return bool.Parse(GetSetting("websvcplugin_enabled", "false")); }
        }

        bool IEnergyMonitorPlugin.Init()
        {
            // get the base url from the (Service) app.config
            baseUrl = GetSetting("websvcplugin_baseurl", "http://localhost/EnergyMonWs.svc");
            return true;
        }

        bool IEnergyMonitorPlugin.Notify(EnergyReading rdg)
        {
            bool retVal = false;

            DateTime ts = rdg.TimeStamp;

            string getUrl = baseUrl + "/set?";
            getUrl += "energy=" + rdg.Energy.ToString() + "&";
            getUrl += "temp=" + rdg.Temperature.ToString() + "&";
            getUrl += "date=" + ts.ToString("dd/MM/yy") + "&";
            getUrl += "time=" + ts.ToShortTimeString();

            WebClient wc = new WebClient();

            try
            {
                wc.DownloadString(getUrl);
                retVal = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error (Notify) : " + ex.Message);
            }

            return retVal;

        }

        bool IEnergyMonitorPlugin.DeInit()
        {
            // nothing to do in here
            return true;
        }

        NameValueCollection IEnergyMonitorPlugin.Configure(NameValueCollection current)
        {
            NameValueCollection retVal = null;

            frmConfig frm = new frmConfig(current);
            DialogResult dlg = frm.ShowDialog();

            if (DialogResult.OK == dlg)
            {
                retVal = new NameValueCollection();
                retVal.Add("websvcplugin_enabled", frm.Enabled.ToString().ToLower());
                retVal.Add("websvcplugin_baseurl", frm.BaseUrl);
            }
            return retVal; 
        }

        #endregion

        private string GetSetting(string key, string defaultValue)
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            string val = defaultValue;
            try
            {
                string v = appSettings.Get(key);
                if (null != v)
                {
                    val = v;
                }
            }
            catch
            {
                // just ignore
            }
            return val;
        }

    }
}

